package com.wisdytech.linkdcs.pda.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.linkdcs.common.BaseUtils;
import com.wisdytech.linkdcs.common.DateUtil;
import com.wisdytech.linkdcs.common.PojoConvertUtil;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dto.MalfunctionSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MApplyRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MProcessRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MalfunctionDemo;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MalfunctionRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MRegisterVo;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MalfunctionVo;
import com.wisdytech.linkdcs.pda.dto.MalfunctionTitleDto;
import com.wisdytech.linkdcs.pda.service.IMalfunctionService;
import com.wisdytech.linkdcs.system.model.SysDict;

/**
 * 故障相关接口
 * @author dlb
 *
 */
@RestController
@RequestMapping("/malfunction")
public class MalfunctionAction {
	
	private Logger logger =LoggerFactory.getLogger(getClass());
	
	@Resource
	private IMalfunctionService malfunctionService;
	
	@Value("${img.localtion}")
	private String location;
	
	@Value("${img.baseUrl}")
	private String baseUrl;
	
	
	@RequestMapping("/getClassifyList")
	public StandardResult getMalfunctionClassify() {
		List<SysDict> sysDicts =
				malfunctionService.getMalfunctionType();
		Map<String, Object> result =BaseUtils.getRetResult("failureClassList", sysDicts);
		
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/getReasonList")
	public StandardResult getMalfunctionReason() {
		List<SysDict> sysDicts =
				malfunctionService.getMalfunctionReason();
		Map<String, Object> result =BaseUtils.getRetResult("failureReasonList", sysDicts);
		
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/getTitleList")
	public StandardResult getMalfunctionTitle() {
		
		Filter filter =new Filter();
		filter.setNum(1);
		filter.setSize(Integer.MAX_VALUE);
		PageInfo<MalfunctionDemo>  pageInfos =
				malfunctionService.listDemoByFilter("", "", filter);
		
		List<MalfunctionTitleDto> lists =
				PojoConvertUtil.convertPojos(pageInfos.getList(), MalfunctionTitleDto.class);
		//TODO 故障类别暂时未使用字典表，后期修改，目前置空
		Map<String, Object> result =BaseUtils.getRetResult("failureTitleList", lists);
		
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/saveMalfunction")
	public StandardResult saveMalfunction(@RequestParam("failure")String  failure,
			@RequestParam("username")String username,@RequestParam(value = "img",required = false)MultipartFile file) {
		
		
		//保存文件
		String contentType = file.getContentType();   //图片文件类型
        String fileName = file.getOriginalFilename();  //图片名字
        String truename=BaseUtils.getUUID()+"."+fileName;
        String relativePath =DateUtil.getCurDateDefaultStr();
        
        try {
			boolean imgResult=BaseUtils.uploadFile(file.getBytes(), location+"/"+relativePath, truename);
			if(!imgResult) {
				return StandardResult.getFailResult("图片保存失败");
			}
		} catch (IOException e) {
			logger.error("保存故障记录图片失败！！",e);
			e.printStackTrace();
			return StandardResult.getFailResult("图片保存失败");
		}
        
		
		MalfunctionRecord record =JSON.parseObject(failure, MalfunctionRecord.class);
		record.setFileName(fileName);
		record.setFilePath(relativePath+"/"+truename);
		
		
		boolean result =malfunctionService.add(record, username);
		
		
		return result?StandardResult.getSuccessResult():
			StandardResult.getFailResult("保存故障记录失败");
	}
	
	@RequestMapping("/getMalfunctionList")
	public StandardResult getMalfunctionList(Filter filter) {
		
//		Filter filter =new Filter();
//		filter.setNum(1);
//		filter.setSize(Integer.MAX_VALUE);
		MalfunctionSearchDto dto =new MalfunctionSearchDto();
		
		PageInfo<MalfunctionVo>  pageInfos =
				malfunctionService.selectAllMalfunctionLists(filter, dto);
		Map<String, Object> result =BaseUtils.getRetResult("listData", pageInfos.getList());
		result.put("totalPage", pageInfos.getPages());
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/getMalfunctionDetail")
	public StandardResult getMalfunctionDetail(String failureId) {
		
		MRegisterVo vo =malfunctionService.getRecordById(failureId);
		vo.setFilePath(baseUrl+"/"+vo.getFilePath());
		Map<String, Object> result =BaseUtils.getRetResult("failure", vo);
		
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/addApplyRecord")
	public StandardResult addApplyRecord(MApplyRecord record, String outSourceFailure,String username) {
		
		MApplyRecord applyRecord =JSON.parseObject(outSourceFailure, MApplyRecord.class);
		
		boolean result =malfunctionService.addApplyRecord(applyRecord, username);
		
		return result ? StandardResult.getSuccessResult():
			StandardResult.getFailResult("保存委外维修失败");
		
	}
	
	@RequestMapping("/addProcessRecord")
	public StandardResult addProcessRecord(String inFactoryFailure,String username) {
		
		MProcessRecord processRecord =JSON.parseObject(inFactoryFailure, MProcessRecord.class);
		
		boolean result =malfunctionService.addProcessRecord(processRecord, username);
		
		return result ? StandardResult.getSuccessResult():
			StandardResult.getFailResult("保存场内维修失败");
		
	}
	
	
}
